<template>
	<z-nav-bar title="设置"></z-nav-bar>
	<!-- #ifdef APP-PLUS || H5 -->
	<view class="item-box">
		<view class="item flex_sbc" @click="clearCache">
			<view>清理缓存</view>
			<view class="flex_fsc">
				<view>{{ cacheSize }}</view>
				<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
			</view>
		</view>
	</view>
	<view class="item-box" @click="toSetting">
		<view class="item flex_sbc">
			<view>权限设置</view>
			<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
		</view>
	</view>
	<view class="item-box" @click="checkUpdate">
		<view class="item flex_sbc">
			<view>升级检查</view>
			<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
		</view>
	</view>
	<!-- #endif -->
	<view class="item-box" @click="cancelAccount">
		<view class="item flex_sbc">
			<view>系统设置</view>
			<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
		</view>
	</view>
	<view class="item-box" @click="beian">
		<view class="item flex_sbc">
			<view>备案</view>
			<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
		</view>
	</view>
	<navigator :url="'/pages/index/my-webview?url=' + settingInfo.about" class="item-box" hover-class="none">
		<view class="item flex_sbc">
			<view>关于</view>
			<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
		</view>
	</navigator>
	<view class="item-box" @click="loginOut">
		<view class="item flex_sbc">
			<view>退出登录</view>
			<image :src="setting.oss_pub_domain + '/app-img/icon/more.png'" class="icon-size-32 uni-ml-12"></image>
		</view>
	</view>
</template>

<script setup>
import { getSettingInfo } from '@/api/my';
import { ref, reactive, getCurrentInstance } from 'vue';
const { proxy: that } = getCurrentInstance();

const settingInfo = reactive({});

getSettingInfo().then((res) => {
	Object.assign(settingInfo, res);
	console.log(res);
});
// #ifdef APP-PLUS || H5

// 升级检查
function checkUpdate() {
	that.tools.checkUpdate(settingInfo, 1)
}

// 全线设置
function toSetting() {
	if (uni.getSystemInfoSync().platform === 'ios') {
		const UIApplication = plus.ios.import('UIApplication');
		const application2 = UIApplication.sharedApplication();
		const NSURL2 = plus.ios.import('NSURL');
		const setting2 = NSURL2.URLWithString('app-settings:');
		application2.openURL(setting2);

		plus.ios.deleteObject(setting2);
		plus.ios.deleteObject(NSURL2);
		plus.ios.deleteObject(application2);
	} else {
		const Intent = plus.android.importClass('android.content.Intent');
		const Settings = plus.android.importClass('android.provider.Settings');
		const Uri = plus.android.importClass('android.net.Uri');
		const mainActivity = plus.android.runtimeMainActivity();
		const intent = new Intent();
		intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
		const uri = Uri.fromParts('package', mainActivity.getPackageName(), null);
		intent.setData(uri);
		mainActivity.startActivity(intent);
	}
}
// #endif

// 缓存处理
const cacheSize = ref('');
// #ifdef APP-PLUS
function getCache() {
	plus.cache.calculate((size) => {
		console.log(size);
		if (size < 1024) {
			cacheSize.value = size + 'B';
		} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
			cacheSize.value = Math.floor((size / 1024) * 100) / 100 + 'KB';
		} else if (size / 1024 / 1024 >= 1) {
			cacheSize.value = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
		}
		console.log(cacheSize);
	});
}
getCache();

function clearCache() {
	uni.showModal({
		title: '确定要清除系统缓存吗？',
		confirmColor: '#ff0000',
		success: (res) => {
			if (res.confirm) {
				plus.cache.clear(() => {
					that.tools.alert('清除成功');
					getCache();
				});
			}
		}
	});
}

// #endif

function loginOut() {
	uni.showModal({
		title: '提示',
		content: '确认要退出登录吗？',
		success: (cc) => {
			if (cc.cancel) return;
			uni.removeStorageSync('token');
			uni.navigateTo({
				url: '/pages/my/login/login'
			});
		}
	});
}

function beian() {
	that.tools.alert('降杆 App备案：京ICP备2023013381号-3A')
}

function cancelAccount() {
	uni.navigateTo({
		url: '/pages/my/setting/system-setting'
	})
}
</script>

<style lang="scss">
page {
	background: $uni-F5F5F5;
}
</style>

<style lang="scss" scoped>
.item-box {
	width: 100vw;
	background: $uni-white;
	box-sizing: border-box;
	padding: 0 32rpx;
	.item {
		width: 100%;
		height: 112rpx;
		border-bottom: 2rpx solid $uni-F5F5F5;
		color: #333336;
		font-size: 32rpx;
	}
	.user-head {
		width: 80rpx;
		height: 80rpx;
		border-radius: 40rpx;
	}
}

.right-box {
	width: 400rpx;
	text-align: right;
}
</style>
